<?php
/*
*	created by cbq in 2017.02.08
*	栏目相关模型
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Column_m extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->table='column';
    }
    //查询单条栏目信息
    public function sel($where=array(),$field="*"){
    	$this->com_where($where);
    	$this->db->select($field);
        $query=$this->db->get($this->table);
     	$data=$query->row_array();
        return $data;
    }
    //多表查询栏目 和 站点信息
    public function sleAllTable($where=array(),$field='*',$order='coltype asc'){
        $this->com_where($where);
        $this->db->select($field);
        $this->db->from($this->table." as A");
        $this->db->join('sites as B','B.id=A.sitesid');
        $query=$this->db->get();
        $data=$query->result_array();
        return $data;
    }
    //查询多条栏目信息
    public function selall($where=array(),$field='*',$order='coltype asc'){
        $this->com_where($where);
        $this->db->select($field);
        $this->db->order_by($order);
        $query=$this->db->get($this->table);
        $data=$query->result_array();
        return $data;
    }
    //添加栏目
    public function add($data){
        $data['addtime']=time();
        //获取栏目最大id值 以便排序
        $max_colidarr= $this->sel(array(),'max(colid) as max_colid');
        if(!empty($max_colidarr)){
            $max_colid=$max_colidarr['max_colid'];
        }else{
            $max_colid=1;
         }
        $data['orderid']=$max_colid;
        $this->db->insert($this->table,$data);
        return $this->db->insert_id();
    }
    //编辑栏目
    public function edit($where,$data){
        $this->com_where($where);
        $this->db->update($this->table,$data);
        return $this->db->affected_rows();
    }
    //删除栏目
    public function del($where){
        $this->com_where($where);
        $this->db->delete($this->table);
        return $this->db->affected_rows();
    }
    //获取栏目下拉父级列表
    public function getslected($data,$selectedid=''){
        $tree=load_class('Tree');
        $tree->init($data,"parentid","colid",$name='name');
        $setconfing['data-sites']='sitesid';
        $tree->setconfing($setconfing);
        $tree->setconfing('','showname');
        return $tree->get_select(0,$selectedid);
    }
     //获取栏目下拉拷贝列表
    public function getCopyslected($data,$selectedid='',$noselected=array()){
        $tree=load_class('Tree');
        $tree->init($data,"parentid","colid",'name');
        $setconfing['data-sites']='sitesid';
        $tree->setconfing($setconfing);
        $tree->setconfing('sitesname','showname');
        return $tree->get_select_article(0,$selectedid,$noselected);
    }
    //排序栏目列表
    public function getsortdata($data){
        $tree=load_class('Tree');
        $tree->init($data,"parentid","colid",$name='name');
        $tree->setconfing('sitesname','');
        return $tree->getsortdata(0);
    }
    //添加文章栏目下拉选择
    public function getColumnselected($selectedid=''){
        //后期添加栏目权限限制
        $column_where['A.coltype']=array('in',array(0,1));
        $column_where['A.iscopy']=array('!=',1);
        $columnlist=$this->column_m->sleAllTable($column_where,'A.*,B.name as sitesname','sitesid asc');
        $tree=load_class('Tree');
        $tree->init($columnlist,"parentid","colid",$name='name');
        $setconfing['data-sites']='sitesid';
        $tree->setconfing($setconfing);
        $tree->setconfing('sitesname','showname');
        $noselectedpid=array('coltype',array(0,2));//不能选择项
        return $tree->get_select_site(0,$selectedid,$noselectedpid);
    }
    //文章列表搜索下拉列表
    public function getArticleselected($selectedid=''){
        //后期添加栏目权限限制
        $column_where['A.coltype']=array('in',array(0,1));
        $column_where['A.iscopy']=array('!=',1);
        $columnlist=$this->column_m->sleAllTable($column_where,'A.*,B.name as sitesname','sitesid asc');
        $tree=load_class('Tree');
        $tree->init($columnlist,"parentid","colid",$name='name');
        $setconfing['data-sites']='sitesid';
        $tree->setconfing($setconfing);
        $tree->setconfing('sitesname','showname');
       // $noselectedpid=array('coltype',array(0,2));//不能选择项
        return $tree->get_select_article(0,$selectedid);
    }
    //添加栏目文章数 （自增和自减）
    public function cloumn_infonum($colid,$inc=1){
        //获取父栏目id集合
        $where['coltype']=array('in',array(0,1));
        $list=$this->selall($where);
        $tree=load_class('Tree');
        $tree->init($list,"parentid","colid",'name');
        $colidarr=$tree->get_parentid_all($colid);
        $colid_str=implode(',',$colidarr);
        $column_where['colid']=array('in',$colid_str);
        if($inc==1){
          $this->db->set('infonum','`infonum`+1',false);
        }else{
          $this->db->set('infonum','`infonum`-1',false);
        }
      $this->com_where($column_where);
      $this->db->update($this->table);
      return $this->db->affected_rows();
    }
}
?>